package com.neulogistics.contrib.transfercenter.api.application.validator;


import com.neulogistics.contrib.transfercenter.api.application.validator.annotation.InList;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class InListValidator  implements ConstraintValidator<InList, Integer> {
    private Set<Integer> allowedValues;

    @Override
    public void initialize(InList annotation) {
        List<Integer> list = new ArrayList<>(annotation.values().length);
        for (int i : annotation.values()){
            list.add(i);
        }
        allowedValues = new HashSet<>(list);
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return allowedValues.contains(integer);
    }


}
